﻿using BanquetPortion.Database;
using BanquetPortion.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BanquetPortion.Services
{
    public class BanquetGiftMoneyBookRepository : IBanquetGiftMoneyBookRepository
    {
        private readonly AppDbContext _context;
        public BanquetGiftMoneyBookRepository (AppDbContext context)
        {
            _context = context;
        }
        public async Task<IEnumerable<BanquetGiftMoneyBook>> GetBanquetGiftMoneyBooksAsync()
        {
            return await _context.BanquetGiftMoneyBooks.ToArrayAsync();
        }
        public async Task<BanquetGiftMoneyBook> GetBanquetGiftMoneyBookByIdAsync(int banquetGiftMoneyBookId)
        {
            return (await _context.BanquetGiftMoneyBooks.FirstOrDefaultAsync(d => d.Id == banquetGiftMoneyBookId));
        }
        public void CreateBanquetGiftMoneyBook(BanquetGiftMoneyBook banquetGiftMoneyBook)
        {
            if(banquetGiftMoneyBook == null)
            {
                throw new EntryPointNotFoundException(nameof(banquetGiftMoneyBook));
            }
            _context.BanquetGiftMoneyBooks.Add(banquetGiftMoneyBook);
        }
        public void DeleteBanquetGiftMoneyBook(BanquetGiftMoneyBook banquetGiftMoneyBook)
        {
            _context.BanquetGiftMoneyBooks.Remove(banquetGiftMoneyBook);
        }
        public async Task<bool> BanquetGiftMoneyBookExistsAsync(int banquetGiftMoneyBookId)
        {
            return await _context.BanquetGiftMoneyBooks.AnyAsync(t => t.Id == banquetGiftMoneyBookId);
        }
        public async Task<bool> SaveAsync()
        {
            return (await _context.SaveChangesAsync() >= 0);
        }
    }
}
